import { createSlice, PayloadAction} from '@reduxjs/toolkit';

import { StatePagingType } from 'storage/redux';
import RequestStatus from 'storage/redux/RequestStatus';
import {builderPagingHandler} from "storage/redux/reducer";
import {fetchAddReportDetail, fetchDeleteReportDetail, fetchUpdateReportDetail, fetchReportDetailPageData} 
    from "storage/redux/async-action/reportDetailAsyncAction";
import UserStorage from "storage/local/UserStorage";

import {ReportDetail} from "type/entity/ReportDetailUser";

/**
 * 报销单详细信息表状态管理类型
 */
export type ReportDetailState = StatePagingType<ReportDetail>

/** 状态初始化数据 */
const initialState: ReportDetail = {
    data: [],
    loading: false,
    requestStatus: RequestStatus.INIT,
    currentPage: 1,
    pageSize: UserStorage.getInstance().getAdminPageConfigFromLocalStore().reportDetailPageSize,
    total: 0,
    totalPage: 10
};

export const reportDetailSlice = createSlice({
    name: 'reportDetail',
    initialState,
    reducers: {
    },
    extraReducers: (builder) => {
        builderPagingHandler(builder, fetchReportDetailPageData);
        /** 添加报销单详细信息表信息 */
        builder.addCase(fetchAddReportDetail.fulfilled, (state, action) => {
            state.requestStatus = RequestStatus.REFRESH;
        });
        /** 修改报销单详细信息表信息 */
        builder.addCase(fetchUpdateReportDetail.fulfilled, (state, action) => {
            state.requestStatus = RequestStatus.REFRESH;
        });
        /** 报销单详细信息表用户信息 */
        builder.addCase(fetchDeleteReportDetail.fulfilled, (state, action) => {
            state.requestStatus = RequestStatus.REFRESH;
        });
    }
});

export const {} = reportDetailSlice.actions;
export default reportDetailSlice.reducer;

